package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.SelSeatAdapter;
import com.winz.cultureapp.adapter.SelSeatSelAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ActivityDesBean;
import com.winz.cultureapp.entity.ActivityListBean;
import com.winz.cultureapp.entity.BookFixBean;
import com.winz.cultureapp.entity.SeatListBean;
import com.winz.cultureapp.entity.request.ActivityBookReq;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 选择座位
 */
public class SelSeatActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.tv_time)
    TextView mTv_time;
    @BindView(R.id.gv_list)
    GridView mGv_list;
    @BindView(R.id.gv_sel)
    GridView mGv_sel;

    private List<SeatListBean.DataBean.ListBean> mDatas_seat = new ArrayList<>();
    private List<SeatListBean.DataBean.ListBean> mDatas_sel = new ArrayList<>();

    private String id;
    private SelSeatAdapter mAdapter;
    private SelSeatSelAdapter mAdapter_sel;

    private Map<Integer,SeatListBean.DataBean.ListBean> map_sel = new HashMap<>();
    private ActivityDesBean.DataBean data;

    @Override
    protected int contentView() {
        return R.layout.activity_sel_seat;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText("");
        mAdapter = new SelSeatAdapter(this,false, R.layout.item_sel_seat);
        mGv_list.setAdapter(mAdapter);
        mGv_list.setVerticalSpacing(30);
        mGv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int postion, long l) {
                // 如果已选状态的,可以进行点击
                if (mDatas_sel.size() >= data.limitBuyNum){
                    showMsg("购买次数已达上限");
                    return;
                }
                if (mDatas_seat.get(postion).seatStatus == 1){ // 可预定
                    if (mDatas_seat.get(postion).isSel){
                        mDatas_seat.get(postion).isSel = false;
                        map_sel.put(postion,null);
                        // 删除已选
                        mDatas_sel.remove(mDatas_seat.get(postion));
                    }else {
                        mDatas_seat.get(postion).isSel = true;
                        map_sel.put(postion,mDatas_seat.get(postion));
                        // 添加已选
                        mDatas_sel.add(mDatas_seat.get(postion));

                    }
                }
                // 刷新UI
                mAdapter.setDatas(mDatas_seat);
                mAdapter_sel.setDatas(mDatas_sel);

            }
        });

        mAdapter_sel = new SelSeatSelAdapter(this,false, R.layout.item_sel_seat_sel);
        mGv_sel.setAdapter(mAdapter_sel);
        mGv_sel.setNumColumns(3);
        mGv_sel.setVerticalSpacing(20);
        mGv_sel.setHorizontalSpacing(20);

        mAdapter_sel.setOnDeleteListener(new SelSeatSelAdapter.OnDeleteListener() {
            @Override
            public void onDelete(int position) {
                position =  (mDatas_sel.get(position).rowNum-1) * data.colTotal + (mDatas_sel.get(position).colNum -1);
                mDatas_seat.get(position).isSel = false;
                map_sel.put(position,null);
                // 删除已选
                mDatas_sel.remove(mDatas_seat.get(position));

                // 刷新UI
                mAdapter.setDatas(mDatas_seat);
                mAdapter_sel.setDatas(mDatas_sel);
            }
        });

    }


    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null){
            if (intent.getExtras() != null){
                id = intent.getExtras().getString(Constants.KEY.ACTIVITY.ITEM_ID);
                String title = intent.getExtras().getString(Constants.KEY.ACTIVITY.ITEM_TITLE);
                data = (ActivityDesBean.DataBean) intent.getSerializableExtra(Constants.KEY.ACTIVITY.DATA);
                mTv_top_title.setText(title);
                mTv_time.setText(data.startTime.substring(0,16) +" - "+ data.endTime.substring(0,16));

                // 初始化gv_list
                mGv_list.setNumColumns(data.colTotal);
                getList();
            }
        }
    }

    private void getList() {
        showProgressDialog();
        HttpUitls.onGet("activity_seat", id, new OnResponeListener<SeatListBean>() {
            @Override
            public void onSuccess(SeatListBean bean) {
                Logger.d("activity_seat==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    mDatas_seat = bean.data.list;

                    mAdapter.setDatas(mDatas_seat);
                } else {
                    showMsg(bean.msg);
                }

                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("activity_seat==>>", error);
            }
        });

    }

    @OnClick({R.id.fl_back, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.tv_ok:
                commit();
                break;
        }
    }

    /**
     * 提交预定
     */
    private void commit() {
        showProgressDialog();
        List<String> seatList = new ArrayList<>();
        Collection<SeatListBean.DataBean.ListBean> values = map_sel.values();
        for (SeatListBean.DataBean.ListBean seatBean :
                values) {
            seatList.add(seatBean.activitySeatId);
        }
        String token = SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN);
        HttpUitls.onPostByToken("activity_book",token, new ActivityBookReq(id,seatList),new OnResponeListener<BookFixBean>() {
            @Override
            public void onSuccess(BookFixBean bean) {
                HttpUitls.isExpire(bean.code);
                Logger.d("activity_book==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0){
                    if (data.money == 0){
                        // 不需要支付
                        showMsg("预定成功");
                        setResult(RESULT_OK);
                        Bundle bundle = new Bundle();
                        bundle.putInt("index",1);
                        startActivity(PersonalRecordActivity.class, true,bundle);
                    }else {
                        // 调支付
                        startActivity(ActivityOrderActivity.class,false);
                    }

                }else {
                    showMsg(bean.msg);
                }

                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("activity_book==>>", error);
            }
        });

    }

}
